package domain

import "time"

const (
	MessageFieldSender       = "Sender"
	MessageFieldReceiver     = "Receiver"
	MessageFieldClub         = "Club"
	MessageFieldParent       = "Parent"
	MessageFieldMarkReadList = "MarkReadList"
)
const (
	MessageTableName = "messages"
)

type MessageType = int64

const (
	// MessageTypeSystem 系统消息
	MessageTypeSystem      MessageType = 0
	// MessageTypeClub 社团消息
	MessageTypeClub                    = 1
	// MessageTypeApplication 申请消息
	MessageTypeApplication             = 2
	// MessageTypeSender 点对点消息
	MessageTypeSender                  = 3
)

type Message struct {
	ID           int64          `json:"id"`
	ParentID     *int64         `json:"-"`
	SenderID     *int64         `json:"-"`
	ClubID       *int64         `json:"-"`
	ReceiverID   *int64         `json:"-"`
	Title        string         `json:"title" gorm:"type:varchar(255);not null"`
	Content      string         `json:"content"`
	Type         MessageType    `json:"type" gorm:"not null;default:0"`
	Extra        *JSON          `json:"extra" gorm:"type:json"`
	CreatedAt    time.Time      `json:"created_at" `
	UpdatedAt    time.Time      `json:"updated_at" `
	Parent       *Message       `json:"parent,omitempty" gorm:"foreignKey:ParentID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	Sender       *User          `json:"sender,omitempty" gorm:"foreignKey:SenderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	Receiver     *User          `json:"receive,omitempty" gorm:"foreignKey:ReceiverID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
	Club         *Club          `json:"club,omitempty" gorm:"foreignKey:ClubID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	MarkReadList []*MessageRead `json:"mark_read_list"`
}

type MessageDTO struct {
	ID           int64             `json:"id"`
	ParentID     *int64            `json:"-"`
	SenderID     *int64            `json:"-"`
	ReceiverID   int64             `json:"-"`
	ClubID       *int64            `json:"-"`
	Title        string            `json:"title" gorm:"type:varchar(255);not null"`
	Content      string            `json:"content"`
	CreatedAt    time.Time         `json:"created_at" `
	UpdatedAt    time.Time         `json:"updated_at" `
	Parent       *MessageDTO       `json:"parent,omitempty" gorm:"foreignKey:ParentID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	Sender       *UserDTO          `json:"sender,omitempty" gorm:"foreignKey:SenderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	Receiver     *UserDTO          `json:"receiver,omitempty" gorm:"foreignKey:ReceiverID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
	Club         *ClubDTO          `json:"club,omitempty" gorm:"foreignKey:ClubID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	MarkReadList []*MessageReadDTO `json:"mark_read_list"`
}
